// index.js
// 获取应用实例
const app=getApp()
Page({
  data:{
    forumdata:[],
    nowmodule:1,
    candianzan:[],
    login:''
  },
  module_1(){
    this.setData({
        nowmodule:1
    })
    this.onShow()
  },
  module_2(){
this.setData({
        nowmodule:2
    })
    this.onShow()
  },
  module_3(){
this.setData({
        nowmodule:3
    })
    this.onShow()
  },
  module_4(){
this.setData({
        nowmodule:4
    })
    this.onShow()
  },
  module_5(){
this.setData({
        nowmodule:5
    })
    this.onShow()
  },
  candianzan(){
    var a=new Array
    let openid = wx.getStorageSync('openid')
       for (var i=0;i<this.data.forumdata.length;i++){
          if (this.data.forumdata[i].like_people.indexOf(openid)<0){
              a[i]=true          
              }
          else{
              a[i]=false
              }
          }
          this.setData({
              candianzan:a
          })
  },
  onPullDownRefresh(){
    wx.startPullDownRefresh()
        },
 async onShow(){
    const db=wx.cloud.database()
    let count=await db.collection("module_"+this.data.nowmodule).count()
    count=count.total
    let all=[]
    for (let i=0;i<count;i+=20){
        let list=await db.collection("module_"+this.data.nowmodule).skip(i).get()
        all = all.concat(list.data)
    }  
    this.setData({
        forumdata:all.reverse()
    })
    if (wx.getStorageSync('user').length==0){
        this.setData({
            login:false
        })
    }else{
        this.setData({
            login:true
        })
    }

  
    },
login(){
    let that=this

    wx.showModal({
        title: '当前未登录',
        content: '请登录',
        showCancel:false,
        success (res) {
          if (res.confirm){
            wx.getUserProfile({
                desc:'必须授权后才能使用',
                success:res=>{
                    var currentTime=new Date(parseInt(Date.parse(new Date()))).toLocaleString().replace(/:\d  {1,2}$/,' ');
                    let user=res.userInfo
                    //检查是否之前授权登陆过
                    wx.cloud.database().collection('login_users').where({
                        _openid:wx.getStorageSync('openid')
                    }).get({
                        success(res){
                            if(res.data.length==0){
                                console.log(currentTime);
                                wx.cloud.database().collection('login_users').add({
                                    data:{
                                        touxiang:user.avatarUrl,
                                        nickName:user.nickName,
                                        time:currentTime
                                    }
                                })
                            }else{
                                console.log(res.data[0]._id);
                                var _id=res.data[0]._id
                                wx.cloud.database().collection('login_users').doc(_id).set({
                                    data:{
                                        touxiang:user.avatarUrl,
                                        nickName:user.nickName,
                                        time:currentTime
                                    }
                                })
                            }
                        }
                    })
                    
                    //把用户信息缓存到本地
                    wx.setStorageSync('user',user);
                    that.setData({
                        login:true
                    })
                    wx.showToast({
                      title: '登陆成功',
                      icon:"success",
                      duration:1500
                    })
                },
                fail:res=>{
                    wx.showToast({
                        title: '登陆失败',
                        icon:"error",
                        duration:1500
                      })
                }
                
            })

    
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    
},
dianzan:function(e){
    var that =this
    if (this.data.candianzan[e.target.dataset.index]==true){
        var a='candianzan['+e.target.dataset.index+']'
        var b='forumdata['+e.target.dataset.index+'].like_num'
        var c='forumdata['+e.target.dataset.index+'].like_people'
        var temp=this.data.forumdata[e.target.dataset.index].like_people
        temp=temp.concat(wx.getStorageSync('openid'));
        
    this.setData({
        [a]:false,
        [b]:this.data.forumdata[e.target.dataset.index].like_num+1,
        [c]:temp
    })
    wx.cloud.database({}).collection('module_'+that.data.nowmodule).doc(that.data.forumdata[e.target.dataset.index]._id).update({
        data:{
            like_num:that.data.forumdata[e.target.dataset.index].like_num,
            like_people:that.data.forumdata[e.target.dataset.index].like_people
    }
    })
}else{
        var a='candianzan['+e.target.dataset.index+']'
        var b='forumdata['+e.target.dataset.index+'].like_num'
        var removeindex = this.data.forumdata[e.target.dataset.index].like_people.indexOf(wx.getStorageSync('openid'))
        var c='forumdata['+e.target.dataset.index+'].like_people'
        var temp=this.data.forumdata[e.target.dataset.index].like_people
        temp.splice(removeindex,1)
    this.setData({
        [a]:true,
        [b]:this.data.forumdata[e.target.dataset.index].like_num-1,
        [c]:temp
    })
    wx.cloud.database({}).collection('module_'+that.data.nowmodule).doc(that.data.forumdata[e.target.dataset.index]._id).update({
        data:{
            like_num:that.data.forumdata[e.target.dataset.index].like_num,
            like_people:that.data.forumdata[e.target.dataset.index].like_people
    }
    },
    
    
    )}
},
previewImage: function (e) { 
    var that=this
    var current=e.target.dataset.src; 
    console.log(e.target);
    wx.previewImage({ 
     current: current, // 当前显示图片的http链接 
     urls: that.data.forumdata[e.target.dataset.father_index].imgurl // 需要预览的图片http链接列表 
    }) 
    
   } ,
   comment:function(e){
        var that=this
        wx.navigateTo({
        url: '/pages/index/neirong/neirong?current=' + JSON.stringify(that.data.forumdata[e.target.dataset.src]),
    })
   }
})

